/*
 * @Author: LAPTOP-STS5K12N\icey 2237665219@qq.com
 * @Date: 2025-01-30 15:19:15
 * @LastEditors: LAPTOP-STS5K12N\icey 2237665219@qq.com
 * @LastEditTime: 2025-01-31 19:22:49
 * @FilePath: \Buzzer\Bsp\src\bsp.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/********************************************************************************
 * @file    bsp.c
 * @author  your name
 * @brief
 *******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "tim.h"
#include "i2c.h"
#include "lo_logic.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
/*
    @brief AT24C02 I2C 设备地址
*/
#define AT24C02_I2C_ADDRESS 0xA0

/* Private macro -------------------------------------------------------------*/

/* Private function prototypes ----------------------------------------------*/

/* Private functions --------------------------------------------------------*/

/* Private variables --------------------------------------------------------*/

/* Public variables ---------------------------------------------------------*/
/*
    BSP结构体
*/
Bsp_t g_Bsp_vars;
/* Public functions ---------------------------------------------------------*/

/**
 * @brief 初始化BSP
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
void Bsp_Init(void)
{
    // 定时器中断初始化
    HAL_TIM_Base_Start_IT(&htim2);

    // 逻辑初始化
    Lo_Logic_Init();

    // 调试模式
    g_Bsp_vars.is_debug = true;
}

/**
 * @brief 定时器中断回调函数
 *
 * @param htim
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{

    if (htim->Instance == TIM2)
    {
        // 处理中断逻辑
        static uint32_t time_count = 0;

        time_count++;
        if (time_count >= 1000)
        {
            time_count = 0;
            g_Bsp_vars.time_count++;
        }

        // 逻辑IO管理
        Lo_Logic_Manage();
    }
}

/**
 * @brief 软断点
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
void Bsp_SoftBreakPoint(void)
{
    if (*((uint32_t *)0xE000EDF0) & 0x00000001) // 判断是否处于在线调试模式
    {
        __breakpoint(0); // 触发断点
    }
}

/**
 * @brief AT24C02 写入数据
 *
 * @param mem_address
 * @param data
 * @param size
 * @return uint8_t
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
uint8_t AT24C02_Write(uint8_t mem_address, uint8_t *data, uint8_t size)
{
    // 发送设备地址（写模式）和存储地址
    if (HAL_I2C_Mem_Write(&hi2c2, AT24C02_I2C_ADDRESS, mem_address, I2C_MEMADD_SIZE_8BIT, data, size, 100) != HAL_OK)
    {
        return 0; // 写入失败
    }
    HAL_Delay(5); // 等待写入完成（AT24C02 写入需要时间）
    return 1;     // 写入成功
}

/**
 * @brief AT24C02 读取数据
 *
 * @param mem_address
 * @param data
 * @param size
 * @return uint8_t
 *
 * @author XX
 * @date 2025-01-30
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-30  XX    First release
 */
uint8_t AT24C02_Read(uint8_t mem_address, uint8_t *data, uint8_t size)
{
    // 发送设备地址（写模式）和存储地址，然后切换到读模式
    if (HAL_I2C_Mem_Read(&hi2c2, AT24C02_I2C_ADDRESS, mem_address, I2C_MEMADD_SIZE_8BIT, data, size, 100) != HAL_OK)
    {
        return 0; // 读取失败
    }
    return 1; // 读取成功
}

/**
 * @brief 重写外部中断回调函数
 *
 * @param GPIO_Pin
 *
 * @author XX
 * @date 2025-01-31
 * @copyright Copyright (c) 2025
 * @version
 *   V1.0  2025-01-31  XX    First release
 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    Lo_Logic_t *lo_logic_item = Get_Lo_Logic_Item(0);
    // 此处需要与s_lo_logic建立映射关系
    for (uint8_t i = 0; i < Lo_Logic_GetNum(); i++)
    {
        if (GPIO_Pin == lo_logic_item->mapping.input.GPIO_Pin)
        {
            break;
        }
    }

    switch (GPIO_Pin)
    {
    case Key_Input_Pin:
        lo_logic_item->state = Lo_Logic_State_Delay;
        break;
    default:
        Bsp_SoftBreakPoint();
        break;
    }
}
/************************ (C) COPYRIGHT Your Company *****END OF FILE****/
